package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.DepartmentQueryRequest;
import com.allen.study.application.api.response.DepartmentQueryResponse;
import com.allen.study.application.repository.IDepartmentReadModelRepo;
import com.allen.study.domain.entity.Department;
import com.allen.study.domain.repository.IDepartmentRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.DepartmentPOAssembler;
import com.allen.study.infrastructure.persistence.dao.IDepartmentMapper;
import com.allen.study.infrastructure.persistence.po.DepartmentPO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门信息表
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class DepartmentRepoImpl extends ServiceImpl<IDepartmentMapper, DepartmentPO>
        implements IDepartmentRepo, IDepartmentReadModelRepo {

    /**
     * 部门信息表持久化对象结构映射
     */
    private final DepartmentPOAssembler departmentPOAssembler;

    @Override
    public List<DepartmentQueryResponse> query(Pagination pagination, DepartmentQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<DepartmentPO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果 父级id 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getParentId())) {
            lambdaQueryWrapper.eq(DepartmentPO::getParentId, queryRequest.getParentId());
        }
        // 如果 部门名称 不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getDepName())) {
            lambdaQueryWrapper.eq(DepartmentPO::getDepName, queryRequest.getDepName());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(DepartmentPO::getId);

        // 分页查询
        Page<DepartmentPO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 部门信息表持久化对象 转 部门信息表查询响应对象
        return departmentPOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public Department queryById(String departmentId) {
        // 根据主键查询部门信息表
        DepartmentPO departmentPO = super.getById(departmentId);
        // 部门信息表持久化对象 转 部门信息表响应对象
        return departmentPOAssembler.toEntity(departmentPO);
    }

    @Override
    public void create(Department department) {
        // 部门信息表实体转部门信息表持久化对象
        DepartmentPO departmentPO = departmentPOAssembler.assembler(department);

        // 创建部门信息表
        super.save(departmentPO);
    }

    @Override
    public void deleteById(String departmentId) {
        // 物理删除部门信息表
        super.removeById(departmentId);
    }

    @Override
    public void updateById(Department department) {
        // 部门信息表实体转部门信息表持久化对象
        DepartmentPO departmentPO = departmentPOAssembler.assembler(department);
        // 更新部门信息表
        super.updateById(departmentPO);
    }
}
